package com.hwua.tonyshop.controller.bg;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hwua.tonyshop.criteria.GoodsInfoCriteria;
import com.hwua.tonyshop.entity.mojo.GoodsInfoAO;
import com.hwua.tonyshop.entity.pojo.GoodsInfo;
import com.hwua.tonyshop.service.IGoodsInfoService;
import com.hwua.tonyshop.util.FileUtil;
import com.slkj.autocode.entity.ServiceResult;
import com.slkj.autocode.util.DateTimeUtils;
/**
 * 
 * @author sunny Yang
 * @date 2018年5月24日
 * @discribe
 * 商品管理
 *
 */
@Controller
@RequestMapping(value = "/back/goods")
public class GoodsManagerController {
	@Resource
	IGoodsInfoService goodsInfoService;
	/**
	 * 跳转到商品管理界面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toGoodsManagerPage", method = { RequestMethod.GET, RequestMethod.POST })
	public String toGoodsManagerPage(Model model,HttpServletRequest request,HttpServletResponse response) {
	  	
		return "background/goods_manage_index";
	}
	/**
	 * 跳转到商品添加界面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toAddGoodsPage", method = { RequestMethod.GET, RequestMethod.POST })
	public String toAddGoodsPage(Model model,HttpServletRequest request,HttpServletResponse response) {
	  	
		return "background/oz_details";
	}
	
	@RequestMapping(value = "/getGoodsInfo", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<GoodsInfoAO> getGoodsInfo(Integer goodsInfoId,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
		return goodsInfoService.selectById(goodsInfoId);
	}
	
	@RequestMapping(value = "/getGoodsInfos", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<List<GoodsInfoAO>> getGoodsInfos(Model model,HttpServletRequest request,HttpServletResponse response) {
		GoodsInfoCriteria criteria = new GoodsInfoCriteria();
		return goodsInfoService.selectByCriteria(criteria);
	}
	
	@RequestMapping(value = "/deleteGoodsInfo", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<Integer> deleteGoodsInfo(Integer goodsInfoId,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
		return goodsInfoService.deleteById(goodsInfoId);
	}
	
	@RequestMapping(value = "/addGoodsInfo", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<Integer> addGoodsInfo(GoodsInfo goodsInfo,@RequestParam("file") MultipartFile file,@RequestParam("file1") MultipartFile file1,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
		String fileName=file.getOriginalFilename();
		String newfileName = System.currentTimeMillis()+"";
		String path = request.getSession().getServletContext().getRealPath("static")+"/background/img/goods/"+newfileName+fileName.substring(fileName.lastIndexOf("."));
		if(FileUtil.uploadFile(file, path)){
			goodsInfo.setTitleImg("background/img/goods/"+newfileName+fileName.substring(fileName.lastIndexOf(".")));
		}
		String fileName1=file1.getOriginalFilename();
		String newfileName1 = System.currentTimeMillis()+"";
		String path1 = request.getSession().getServletContext().getRealPath("static")+"/background/img/goods/"+newfileName1+fileName1.substring(fileName1.lastIndexOf("."));
		if(FileUtil.uploadFile(file1, path1)){
			goodsInfo.setGoodsImg("background/img/goods/"+newfileName1+fileName1.substring(fileName1.lastIndexOf(".")));
		}
		goodsInfo.setState("0");
		goodsInfo.setCreateDate(DateTimeUtils.formateDateToStr(new Date()));
		return goodsInfoService.addGoodsInfo(goodsInfo);
	}
	
	@RequestMapping(value = "/updateGoodsInfo", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<Integer> updateGoodsInfo(GoodsInfo goodsInfo,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
		return goodsInfoService.updateById(goodsInfo);
	}
}
